﻿using System;
using System.ServiceModel.Channels;
using System.Xml;
using System.Diagnostics;
using System.IO;

namespace Microsoft.Biztalk.Adapters.Wcf.NullBinding
{
    static class Helpers
    {
        public const string BindingScheme="null";
        public const string Action = "http://bts.adapters.wcf/nullAction";
        public const string Address = BindingScheme + "://nullAddress/";
        public const string EventSource = "Biztalk-WcfAdapter-NullBinding";

        public static void LogReceivedMessage(Message message, bool logMessages, MessageType type)
        {
            if (!logMessages)
            {
                using (XmlDictionaryWriter writer = new NullXmlDictionaryWriter())
                {
                    message.WriteMessage(writer);
                }
            }
            else
            {
                MemoryStream stream = new MemoryStream();
                using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream))
                {
                    message.WriteMessage(writer);
                    writer.Flush();
                    StreamReader reader = new StreamReader(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    EventLog.WriteEntry(EventSource, "Received message of type : " + type.ToString() + "\n\n " + reader.ReadToEnd());
                }
            }
        }

        public static void LogCreatedMessage(Message message, bool logMessages, MessageType type)
        {            
            if (!logMessages)
                return;
            EventLog.WriteEntry(EventSource, "Created message of type : " + type.ToString());
        }
    }

    enum MessageType
    {
        Request,
        Reply,
        Input,
        Output
    }
}
